import Joi from 'joi';
import debugCheck from '@kuiper-helpers/debug-check';
import * as validation from '../../common/validation';
import findManyFilter from '../../common/validation/findManyFilter';

export const pagination = {
	tags: ['api', 'notificationSendLog'],
	description: '告警推送记录列表-分页查看',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.object({
			page: Joi.number().default(1).description('当前页'),
			perPage: Joi.number().default(10).description('每页条目数'),
			filter: findManyFilter.notificationSendLog
				.append({
					startTimeGTE: Joi.date().description('开始时间大于等于'),
					startTimeLTE: Joi.date().description('开始时间小于等于'),
					endTimeGTE: Joi.date().description('结束时间大于等于'),
					endTimeLTE: Joi.date().description('结束时间小于等于'),
					usernameSearch: Joi.string().description('用户名搜索'),
					targetSearch: Joi.string().description('地址搜索'),
				})
				.unknown()
				.description('过滤条件'),
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				NotificationSendLogPagination: Joi.object({
					count: Joi.number().description('总条数'),
					pageInfo: Joi.object({
						currentPage: Joi.number().description('当前页数'),
						perPage: Joi.number().description('每页条数'),
						pageCount: Joi.number().description('总页数'),
						itemCount: Joi.number().description('总条数'),
						hasNextPage: Joi.boolean().description('是否有下一页'),
						hasPreviousPage: Joi.boolean().description('是否有上一页'),
					}),
					items: Joi.array().items(
						validation.notificationSendLogWithId.append({
							user: Joi.object({
								_id: Joi.string(),
								username: Joi.string(),
							}),
							receivers: Joi.array()
								.items(
									Joi.object({
										type: Joi.string()
											.meta({
												_mongoose: { enum: ['wechat', 'email', 'phone'] },
											})
											.description('邮箱/手机/微信'),
										contact: Joi.string()
											.allow('', null)
											.description('联系地址'),
										status: Joi.string().allow(null).description('发送状态'),
										statusCode: Joi.string()
											.allow(null)
											.description('发送状态码'),
									})
								)
								.description('接收列表'),
						})
					),
				}),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload: { page, perPage, filter },
				} = request;

				return JSON.stringify({
					variables: { page, perPage, filter },
					// #region query
					query: `
						query($page: Int = 1, $perPage: Int = 10, $filter: FilterFindManyNotificationSendLogInput, $sort: SortFindManyNotificationSendLogInput = _ID_DESC) {
							NotificationSendLogPagination(page: $page, perPage: $perPage, filter: $filter, sort: $sort) {
								count
								pageInfo {
									currentPage
									perPage
									pageCount
									itemCount
									hasNextPage
									hasPreviousPage
								}
								items {
									_id
									refId
									status
									taskType
									taskCategory
									target
									domain
									uid
									startTime
									endTime
									title
									summary
									content
									receivers {
										type
										contact
										status
										statusCode
									}
									user {
										_id
										username
									}
								}
							}
						}`,
					// #endregion
				});
			},
		},
	},
};

export const count = {
	tags: ['api', 'notificationSendLog'],
	description: '告警推送记录数量统计',
	auth: debugCheck('disable-open-api-auth')
		? false
		: {
				mode: 'required',
				access: {
					// 只允许使用 URL Token 访问
					entity: 'app',
				},
		  },

	validate: {
		payload: Joi.object({
			filter: findManyFilter.notificationSendLog,
		}),
	},

	response: {
		schema: Joi.object({
			data: Joi.object({
				NotificationSendLogCount: Joi.number().description('数量'),
			}),
		}).label('Response'),
	},

	handler: {
		injectGraphql: {
			payloadBuilder(request) {
				const {
					payload: { filter },
				} = request;

				return JSON.stringify({
					variables: { filter },
					// #region query
					query: `
						query($filter: FilterCountNotificationSendLogInput) {
							NotificationSendLogCount(filter: $filter)
						}`,
					// #endregion
				});
			},
		},
	},
};
